package com.bjdd.jdy.ui.ready;

import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjdd.jdy.App;
import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.User;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;

import butterknife.Bind;
import butterknife.OnClick;

public class ReadyActivity extends BaseActivity<ReadyPresenter, ReadyModel> implements ReadyContract.View {


    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.tv_toolbar_right)
    TextView tvChangeUser;
    @Bind(R.id.tc_ready_material_status)
    TextView tcReadyMaterialStatus;
    @Bind(R.id.ll_ready_order1)
    LinearLayout llReadyOrder1;
    @Bind(R.id.img_ready_exam1)
    ImageView imgReadyExam1;
    @Bind(R.id.img_ready_exam2)
    ImageView imgReadyExam2;
    @Bind(R.id.img_ready_exam3)
    ImageView imgReadyExam3;
    @Bind(R.id.ll_ready_order3)
    LinearLayout llReadyOrder3;
    private User user;
    @Override
    public int getLayoutId() {
        return R.layout.activity_ready;
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.getUserInfo(SpUtil.getToken(), SpUtil.getUser().getCarrierId());
    }

    @Override
    public void initView() {
        L.d("jdy","rinimayo");
        StatusBarUtil.setColor(this,getResources().getColor(R.color.toolbar_bg),0);
        title.setText("成为自由人");
        tvChangeUser.setText("切换账号");
        tvChangeUser.setVisibility(View.VISIBLE);
    }

    @Override
    public void setUser() {
        user = SpUtil.getUser();
        switch (user.getMaterialStatus()){
            case 0:
                //未提交审核
                llReadyOrder1.setClickable(true);
                tcReadyMaterialStatus.setText(getResources().getString(R.string.material_no_commit));
                tcReadyMaterialStatus.setClickable(true);
                break;
            case 1:
                //审核中
                llReadyOrder1.setClickable(false);
                tcReadyMaterialStatus.setText(getResources().getString(R.string.material_ing));
                tcReadyMaterialStatus.setClickable(false);
                break;
            case 2:
                //审核成功
                llReadyOrder1.setClickable(false);
                tcReadyMaterialStatus.setText(getResources().getString(R.string.material_success));
                tcReadyMaterialStatus.setClickable(false);
                break;
            case 3:
                //审核失败
                llReadyOrder1.setClickable(true);
                tcReadyMaterialStatus.setText(getResources().getString(R.string.material_failed));
                tcReadyMaterialStatus.setClickable(true);
                break;
        }
        L.d("考试状态："+user.getExamStatus());
        switch (user.getExamStatus()){
            case 0:
                //还未开始考试
                imgReadyExam1.setBackgroundResource(R.mipmap.list_img_section_1_f);
                imgReadyExam2.setBackgroundResource(R.mipmap.list_img_section_2_f);
                imgReadyExam3.setBackgroundResource(R.mipmap.list_img_section_3_f);
                break;
            case 1:
                //通过第一阶段考试
                imgReadyExam1.setBackgroundResource(R.mipmap.list_img_section_1_o);
                imgReadyExam2.setBackgroundResource(R.mipmap.list_img_section_2_f);
                imgReadyExam3.setBackgroundResource(R.mipmap.list_img_section_3_f);
                break;
            case 2:
                //通过第二阶段考试
                imgReadyExam1.setBackgroundResource(R.mipmap.list_img_section_1_o);
                imgReadyExam2.setBackgroundResource(R.mipmap.list_img_section_2_o);
                imgReadyExam3.setBackgroundResource(R.mipmap.list_img_section_3_f);
                break;
            case 3:
                //通过第三阶段考试
                imgReadyExam1.setBackgroundResource(R.mipmap.list_img_section_1_o);
                imgReadyExam2.setBackgroundResource(R.mipmap.list_img_section_2_o);
                imgReadyExam3.setBackgroundResource(R.mipmap.list_img_section_3_o);
                break;
            case 4:
                imgReadyExam1.setBackgroundResource(R.mipmap.list_img_section_1_o);
                imgReadyExam2.setBackgroundResource(R.mipmap.list_img_section_2_o);
                imgReadyExam3.setBackgroundResource(R.mipmap.list_img_section_3_o);
                break;
        }
    }

    @OnClick({R.id.all_to_main,R.id.img_ready_material,R.id.ll_ready_order1,R.id.tv_toolbar_right, R.id.img_ready_exam1, R.id.img_ready_exam2, R.id.img_ready_exam3, R.id.ll_ready_order3,R.id.toolbar_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_ready_order1:
                JumpUtil.toCommit(context);
                break;
            case R.id.img_ready_exam1:
                if (user.getMaterialStatus() == 0){
                    T.show("请先提交资料");
                }else {
                    JumpUtil.toVideoExam(context,1);
                }
                break;
            case R.id.img_ready_material:
                if (user.getMaterialStatus() == 0){
                    T.show("请先提交资料");
                }else {
                    JumpUtil.toVideoExam(context,1);
                }
                break;
            case R.id.img_ready_exam2:
                if (user.getExamStatus() < 1){
                    T.show("请完成第一节学习");
                }else {
                    JumpUtil.toVideoExam(context,2);
                }
                break;
            case R.id.img_ready_exam3:
                if (user.getExamStatus() < 2){
                    T.show("请先完成第二节学习");
                }else {
                    JumpUtil.toVideoExam(context,3);
                }
                break;
            case R.id.ll_ready_order3:
                if (user.getExamStatus() < 3){
                    T.show("请先完成第二步学习");
                }else if (user.getExamStatus() == 4){
                    T.show("已通过考试");
                }else {
                    JumpUtil.toExam(context);
                }
                break;
            case R.id.toolbar_back:
                if((System.currentTimeMillis()-exitTime) > 2000){
                    T.show("再按一次退出程序");
                    exitTime = System.currentTimeMillis();
                } else {
                    finish();
                    App.getApp().exit();
                }
                break;
            case R.id.tv_toolbar_right:
                JumpUtil.toLogin(context);
                finish();
                break;
            case R.id.all_to_main:
                if (user.getExamStatus() == 4 && user.getMaterialStatus() == 2){
                    JumpUtil.toMain(context);
                }
                break;
        }
    }
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                T.show("再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                App.getApp().exit();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
